package com.example.ffepeg.yourAudio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class YourAudio {
    public static void main(String[] args) {
       /* try {
            String ffmpegPath = "ffmpeg";
            String inputFile = "https://cdn1.suno.ai/62002194-af18-4724-a0f7-09eb4246d63b.mp4";
            String audioFile = "new_audio.mp3";
            String outputFile = "output_video.mp4";

            ProcessBuilder processBuilder = new ProcessBuilder(
                    ffmpegPath, "-i", inputFile, "-i", audioFile,
                    "-c:v", "copy", "-map", "0:v:0", "-map", "1:a:0",
                    "-shortest", outputFile
            );

            // 运行命令
            Process process = processBuilder.inheritIO().start();
            int exitCode = process.waitFor();

            if (exitCode == 0) {
                System.out.println("音频替换成功！");
            } else {
                System.out.println("音频替换失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        String audioUrl = "http://allapi.natapp1.cc/files/6ac3a3f3-942e-4d9d-b878-8587d8e4da9d.mp4";
        String outputPath = "src/main/resources/out/output486.mp4";
        //RemoveAudioFromMp4(audioUrl, outputPath);


        String in = "http://allapi.natapp1.cc/files/67c61ce1-a8b4-4bf1-92ae-a323004a21e8.mp4";
        String wav = "https://pushimg.liecaizhijia.com/temporary_voic/2025-02-11/ca818acb-62c7-4426-9efb-47dad653039d.wav";
        //AddAudioToVideo(in,wav);

    }


    /**
     * 去除原音频中的音频
     * @param audioUrl  音频路径
     * @param outputPath 输出路径
     * @return
     */
    public static void RemoveAudioFromMp4(String audioUrl, String outputPath){
        try {
            // 创建并配置ProcessBuilder
            ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", audioUrl, "-c:v", "copy", "-an", outputPath);
            pb.redirectErrorStream(true); // 合并标准错误和标准输出

            // 开始进程
            Process process = pb.start();
            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待命令执行完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("音频移除成功！");
            } else {
                System.err.println("音频移除失败，退出码：" + exitCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void AddAudioToVideo(String videoFilePath,String audioFilePath){
        String outputFilePath = "src/main/resources/out/video_with_audio.mp4";

        // 创建 ProcessBuilder 对象并设置 FFmpeg 命令
        ProcessBuilder processBuilder = new ProcessBuilder(
                "ffmpeg",
                "-i", videoFilePath,   // 输入视频文件
                "-i", audioFilePath,   // 输入音频文件
                "-codec", "copy",      // 直接复制视频和音频流而不重新编码
                outputFilePath         // 输出文件路径
        );

        try {
            // 启动进程
            Process process = processBuilder.inheritIO().start();
            // 等待进程完成
            int exitCode = process.waitFor();
            System.out.println("FFmpeg process exited with code " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}
